<?php
namespace Common\Model;
use Common\Model\VRNewsModel;
use Admin\Controller\CompanyController;
class ResourceLibraryModel extends VRNewsModel
{
	
	protected $_validate = array(
	);
	
	function getResourcesList($show_type, $formParam, $pageStart, $pageSize){
		$resultData = array();
		$where = array();
		$all_where = array();
//		$admin_user_model = M('vradmin1.user','admin_');
		
// 		if(isset($formParam['creatorName'])){
// 			if($formParam['creatorName']){
// 				$admin_user_info = $admin_user_model->where('real_name like "%'.$formParam['creatorName'].'%" ')->find();
// 				if($admin_user_info){
// 					$where['creator_id'] = array('EQ',$admin_user_info['admin_id']);
// 				}else{
// 					$where['creator_id'] = '-1';
// 				}
// 			}
// 		}

		if($formParam['Name']){
			 $keyword = $formParam['Name'];
			 $where['file_name'] = array('LIKE',"%$keyword%");
		}
		if($formParam['type']){
			$all_where['type'] = array('EQ',$formParam['type']);
			$where['type'] = array('EQ',$formParam['type']);
// 			if($formParam['type'] == '2' && $show_type == '2'){
// 				$all_where['status'] = '2';
// 				$where['status'] = '2';
// 			}
		}
		if($formParam['is_water'] != '') $where['is_water'] = array('EQ',$formParam['is_water']);
		if($formParam['startDate']) $where['create_time'] =array('egt',$formParam['startDate']);
		if($formParam['endDate']) $where['create_time'] = array('elt',$formParam['endDate']);
		if($formParam['order']) $order = $formParam['order'];
		if($formParam['operation_id']){
			if($formParam['operation_id'] !='1'){
				$where['operation_id'] = $formParam['operation_id'];
			}
		}

		$count_Total = $this->where($where)->count();
		$resources_list = $this->where($where)
		->order("create_time $order, resource_id desc")
		->limit($pageStart, $pageSize)
		->select();

		$new_data = array();
		foreach($resources_list as $key=>$value){
			$user_model = new AdminUserModel();
			$nickname = $user_model->where('admin_id = '.$value['operation_id'])->getField('real_name');
			$company_id = $user_model->where('admin_id = '.$value['operation_id'])->getField('company_id');
			if($nickname){
				if($company_id){
					$company_model = new CompanyModel();
					$company_name = $company_model->where('company_id = '.$company_id)->getField('name');
					$resources_list[$key]['operation_name'] = $company_name.":".$nickname;
				}else{
					$resources_list[$key]['operation_name'] = $nickname;
				}	
			}
			$new_data[] = $resources_list[$key];
		}
// 		$all_resources_list = $this->where($all_where)
// 		->order("create_time $order, resource_id desc")
// 		->select();
		
// 		foreach($resources_list as $key=>$value){
// 			$resources_list[$key]['creator_name'] = '';
// 			$real_name = $admin_user_model->where('admin_id = '.$value['creator_id'])->getField('real_name');
// 			if($real_name) $resources_list[$key]['creator_name'] = $real_name;
// 		}
		//$resultData['all_data'] = $all_resources_list;
		$resultData['data'] = $new_data;
		$resultData['count_Total'] = $count_Total;
		return $resultData;
	}
	
}

